﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntitiesLayer;
using System.Collections;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;

namespace DAL.Repositories
{
    public class GimnasioRepository : IRepository<Gimnasio>
    {
        public Gimnasio GetById(int pid)
        {
            Gimnasio gimnasio = null;
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_consultar_gimnasio");

            if (ds.Tables[0].Rows.Count > 0)
            {
                var dr = ds.Tables[0].Rows[0];
                gimnasio = new Gimnasio()
                {
                    Id = Convert.ToInt32(dr["id"]),
                    Nombre = dr["nombre"].ToString(),
                    Ubicacion = dr["ubicacion"].ToString(),
                    Telefono = dr["telefono"].ToString(),
                    CedulaJuridica = dr["cedula_juridica"].ToString(),
                    Logo = dr["logo"].ToString()
                };
            }
            return gimnasio;
        }

        private List<Gimnasio> _insertItems;
        private List<Gimnasio> _deleteItems;
        private List<Gimnasio> _updateItems;
        public void Save()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    if (_insertItems.Count > 0)
                    {
                        foreach (Gimnasio objGimnasio in _insertItems)
                        {
                            //InsertGimnasio(objGimnasio);
                        }
                    }

                    if (_updateItems.Count > 0)
                    {
                        foreach (Gimnasio p in _updateItems)
                        {
                            UpdateGimnasio(p);
                        }
                    }

                    if (_deleteItems.Count > 0)
                    {
                        foreach (Gimnasio p in _deleteItems)
                        {
                            DeleteGimnasio(p);
                        }
                    }

                    scope.Complete();
                }
                catch (TransactionAbortedException ex)
                {

                }
                catch (ApplicationException ex)
                {

                }
                finally
                {
                    Clear();
                }

            }
        }
        public void Clear()
        {
            _insertItems.Clear();
            _deleteItems.Clear();
            _updateItems.Clear();
        }
        public void Insert(Gimnasio entity)
        {
            _insertItems.Add(entity);
        }

        public void Delete(Gimnasio entity)
        {
            _deleteItems.Add(entity);
        }

        public void Update(Gimnasio entity)
        {
            _updateItems.Add(entity);
        }
        private void UpdateGimnasio(Gimnasio objGimnasio)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();

                cmd.Parameters.Add(new SqlParameter("@id", objGimnasio.Id));
                cmd.Parameters.Add(new SqlParameter("@nombre", objGimnasio.Nombre));
                cmd.Parameters.Add(new SqlParameter("@ubicacion", objGimnasio.Ubicacion));
                cmd.Parameters.Add(new SqlParameter("@telefono", objGimnasio.Telefono));
                cmd.Parameters.Add(new SqlParameter("@cedulaJuridica", objGimnasio.CedulaJuridica));
                cmd.Parameters.Add(new SqlParameter("@logo", objGimnasio.Logo));


                DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_modificar_gimnasio");

            }
            catch (Exception ex)
            {

            }
        }
        public IEnumerable<Gimnasio> GetAll()
        {


            List<Gimnasio> listaGimnasios = null;

            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_consultar_gimnasio");



            if (ds.Tables[0].Rows.Count > 0)
            {
                listaGimnasios = new List<Gimnasio>();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    listaGimnasios.Add(new Gimnasio
                    {
                        Id = Convert.ToInt32(dr["id"]),
                        Nombre = dr["nombre"].ToString(),
                        Ubicacion = dr["ubicacion"].ToString(),
                        Telefono = dr["telefono"].ToString(),
                        CedulaJuridica = dr["cedula_juridica"].ToString(),
                        //Logo = (Byte[])dr["logo"]
                    });
                }
            }

            return listaGimnasios;
        }
        public IEnumerable<Gimnasio> GetUsuarios(int ptipo)
        {
            return null;
        }


        private void DeleteGimnasio(Gimnasio objGimnasio)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@", objGimnasio.Id));
                DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_borrar_Gimnasio");

            }
            catch (SqlException ex)
            {
                //logear la excepcion a la bd con un Exception


            }
            catch (Exception ex)
            {
                //logear la excepcion a la bd con un Exception

            }
        }
    }
}
